package org.foxas.map
{
	import flash.display.Sprite;
	import flash.text.TextFormatAlign;
	
	import org.foxas.base.FFunc;
	import org.foxas.display.FSprite;

	public class FMap extends FSprite
	{
		private var data_r:FMapData;
		public var gridSize:int = 30;
		private var alignSize:int = 0;
		protected var lvGrids:Sprite = new Sprite();
		protected var grids:Array = new Array();
		protected function set alignType(_v:String):void{
			if(_v==TextFormatAlign.LEFT){
				alignSize = 0;
			}else if(_v==TextFormatAlign.CENTER){
				alignSize = this.gridSize/2;
			}
		}
		public function set data(_data:FMapData):void{
			data_r = _data;
			refur();
		}
		public function get data():FMapData{
			return data_r;
		}
		public function setStyle(_gridSize:int=30,_alingType:String=TextFormatAlign.LEFT):void{
			gridSize = _gridSize;
			alignType = _alingType;
			refur();
		}
		public function FMap(_data:FMapData=null,_x:int=0, _y:int=0)
		{
			super(_x, _y);
			this.data = _data;
			this.addChild(lvGrids);
		}
		public function clear1():void{
		 	FFunc.removeAllIn(lvGrids);
		 	for(var _row:int=0;_row<data.rowLength;_row++){
				//cellDatas[_row] = new Array();
				grids[_row] = [];
			}
			grids=[];
		}
		public function refur():void{
			if(data==null){
				return;
			}
			clear1();
			for(var _row:int=0;_row<data.rowLength;_row++){
				//cellDatas[_row] = new Array();
				grids[_row] = new Array();
				for(var _col:int=0;_col<data.colLength;_col++){
					grids[_row][_col] = newGrid(data.getState(_row,_col),this.gridSize,_col*gridSize,_row*gridSize)
					grids[_row][_col].row = _row;
					grids[_row][_col].col = _col;
					lvGrids.addChild(grids[_row][_col]);
				}
			}
		}
		public function show():void{
			this.lvGrids.visible = true;
		}
		public function hidden():void{
			this.lvGrids.visible = false;
		}
		protected function newGrid(_state:int,_size:int,_x:int,_y:int):FMapGrid{
			return new FMapGrid(_state,_size,_x,_y);
		}
		//
		public function getStateByXY(_x:Number,_y:Number):int{
			return data.getState(this.getRowByY(_y),this.getColByX(_x));
		}
		/**
		 * count x from order
		 * @param _pos
		 * @return 
		 * 
		 */		
		public function getXByOrder(_order:int):int{
			return _order%this.data.colLength*(gridSize);
		}
		public function getYByOrder(_order:int):int{
			return int(_order/this.data.colLength)*(gridSize);
		}
		public function getOrderByXY(_x:int,_y:int):int{
			return getRowByY(_y)*this.data.colLength+getColByX(_x);
		}
		/**
		 * @param _x
		 * @return 
		 */		
		public function getColByX(_x:int):int{
			return Math.floor((_x-this.alignSize)/gridSize);
		}
		/**
		 * @param _y
		 * @return 
		 */		
		public function getRowByY(_y:int):int{
			return Math.floor((_y-this.alignSize)/gridSize);
		}
		public function getXByCol(_col:int):int{
			return _col*gridSize+this.alignSize;
		}
		public function getYByRow(_row:int):int{
			return _row*gridSize+this.alignSize;
		}
	}
}